package com.grace.controller;

import com.grace.entity.Student;
import com.grace.service.StudentService;
import com.grace.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/ToEditStudentServlet")
public class ToEditStudentServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取sid
        String sid = request.getParameter("sid");
        //调用service方法
        Student student = studentService.selectStudentById(Long.parseLong(sid));

        //处理日期问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date oldBirthday = student.getBirthday();
        String newBirthday = simpleDateFormat.format(oldBirthday);
        System.out.println(oldBirthday);
        request.setAttribute("birthday", newBirthday);

        //存入request中
        request.setAttribute("student", student);
        //请求转发
        request.getRequestDispatcher("/edit.jsp").forward(request, response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
